package gov.pr.celepar.sentinela.admin.dao.postgre;

import gov.pr.celepar.sentinela.admin.dao.MetodoDAO;
import gov.pr.celepar.sentinela.admin.dao.factory.DAOFactoryServer;
import gov.pr.celepar.sentinela.admin.pojo.Metodo;
import gov.pr.celepar.sentinela.util.JDBCLogger; //import gov.pr.celepar.sentinela.admin.pojo.Grupo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PSQLMetodoDAO implements MetodoDAO {

	private static Log logger = LogFactory.getLog(PSQLMetodoDAO.class);

	protected PSQLMetodoDAO() {
	}

	public Collection<Metodo> getMetodos() throws Exception {
		Collection<Metodo> c = new ArrayList<Metodo>();
		PreparedStatement sql = null;
		DAOFactoryServer dao = DAOFactoryServer.getDAOFactory();
		Connection conn = dao.createConnection();
		try {
			StringBuffer s = new StringBuffer();
			s.append(" select codmetodo, nomemetodo, descmetodo").append(
					" from metodo ").append(" order by nomemetodo");

			sql = conn.prepareStatement(s.toString());
			ResultSet rs = sql.executeQuery();
			JDBCLogger.logSQL(sql);
			while (rs.next()) {
				Metodo m = new Metodo();
				m.setCodMetodo(rs.getLong("codmetodo"));
				m.setNomeMetodo(rs.getString("nomemetodo"));
				m.setDescMetodo(rs.getString("descmetodo"));
				c.add(m);
			}
		} catch (Exception e) {
			logger.error("", e);
			throw e;
		} finally {
			if (sql != null)
				sql.close();
			dao.closeConnection(conn);
		}
		return c;
	}
}
